package com.rahwi.rahwi_server.Controller;

import com.rahwi.rahwi_server.Entity.Post;
import com.rahwi.rahwi_server.Handler.Security.Security;
import com.rahwi.rahwi_server.Service.impl.HomePostService;
import com.rahwi.rahwi_server.Service.impl.PostService;
import com.rahwi.rahwi_server.Utils.Converter.PostConverter;
import com.rahwi.rahwi_server.vo.data.BasePost;
import com.rahwi.rahwi_server.vo.data.BaseUser;
import com.rahwi.rahwi_server.vo.data.DetailPost;
import com.rahwi.rahwi_server.vo.data.IPost;
import com.rahwi.rahwi_server.vo.params.PageParams;
import com.rahwi.rahwi_server.vo.params.PostParams;
import com.rahwi.rahwi_server.vo.results.ActionResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;

@RestController
@RequestMapping("/posts")
public class PostController {

    @Autowired
    PostService postService;

    @Autowired
    HomePostService homePostService;

    /**
     * @param pageParams
     * @return PostResult
     * 分页查询所有
     */
    @PostMapping("/pages")
    public ActionResult getPostsPageList(@RequestBody PageParams pageParams) {
        IPost[] posts = PostConverter.toVoPost(
                postService.getPostByPage(pageParams),
                this::toBase
        ).toArray(new IPost[0]);
        return ActionResult.success(posts);
    }


    @PostMapping("/userpost")
    public ActionResult getPostByUserId_page(@RequestParam(name = "uid") Integer uid,
                                             @RequestBody PageParams pageParams){
        if(pageParams == null){
            return ActionResult.fail(403,"参数错误");
        }
        else {
            List<Post> posts = postService.getPostByUidAndPage(uid,pageParams);
            return ActionResult.success(PostConverter.toVoPost(posts,this::toBase));
        }
    }
    /**
     * @param currentPage,tag,sorted
     * @return PostResult
     * dn
     */

    @PostMapping("/homepages")
    public ActionResult getHomePostsPageList(@RequestParam(name = "currentPage")int currentPage,@RequestParam(name = "tag")String tag,@RequestParam(name = "sorted")String sorted) {
        PageParams pageParams = new PageParams();
        pageParams.setPage(currentPage-1);
        IPost post = PostConverter.toVoHomePost(homePostService.getHomePostByPage(pageParams),pageParams.getSize());
        return ActionResult.success(post);
    }

    /**
     * @param
     * @return PostNumber
     * 返回帖子总数
     * dn
     */
    @PostMapping("/postnumber")
    public ActionResult getPostnumber() {
        IPost post = PostConverter.toVoPostNumber(homePostService.getPostNumber());
        return ActionResult.success(post);
    }

    /**
     * @param
     * @return PostNumber
     * 返回帖子总数
     */
    @Security
    @PostMapping("/send/post")
    public ActionResult sendPost(@RequestBody PostParams postParams) {
        String picture;
        if(postParams.getPictures().length <= 0)
            picture = "";
        else
            picture = postParams.getPictures()[0];
        int pid = homePostService.sendPost(postParams.getUid(), postParams.getContent(), picture, postParams.getTag(), postParams.getTitle());
        return pid==0?ActionResult.fail(700,"创建失败"):ActionResult.success(pid);
    }


    /**
     *
     * @param pid
     * @return PostResult
     * 查询指定Id
     */
    @RequestMapping("/detail/{id}")
    public ActionResult getPostById(@PathVariable("id") int pid) {
        IPost post = PostConverter.toVoPost(postService.getPostById(pid),this::toBase);
        if (post == null) {
            return ActionResult.fail(801, "查无此id");
        } else {
            return ActionResult.success(post);
        }
    }

    /**
     *
     * @param tag
     * @param pageParams
     * @return PostResult
     * 查询指定标签
     */
    @RequestMapping("/tag")
    public ActionResult getPostsByTagPageList(String tag, @RequestBody PageParams pageParams){
        IPost[] posts = PostConverter.toVoPost(postService.getPostByTagAndPage(tag,pageParams),
                        this::toBase).toArray(new IPost[0]);
        if(posts.length == 0){
            return ActionResult.fail(802,"查无结果");
        }
        else {
            return ActionResult.success(posts);
        }
    }

    /**
     * 添加一个post
     * @param post
     * @return
     */
    @Security
    @PostMapping("/add")
    public ActionResult addPost(@RequestBody Post post){
        postService.addPost(post);
        return ActionResult.success(post);
    }

    /**
     * 修改一个post
     * @param post
     * @return
     */
    @Security
    @PutMapping("/modify")
    public ActionResult modifyPost(@RequestBody Post post){
        postService.modifyPost(post);
        return ActionResult.success(post);
    }

    /**
     * 根据id删除
     * @param pid
     * @return
     */
    @Security
    @DeleteMapping("/delete/{id}")
    public ActionResult deletePost(@PathVariable("id") int pid){
        postService.deletePostById(pid);
        return ActionResult.success(pid);
    }

    /**
     * MVC测试
     * @return
     */
    @RequestMapping("/test")
    public String test(){
        String st = "avc";
        String res = st;
        return res;
    }

    /**
     * 实体类到vo映射
     * @param post
     * @return
     */
    public BasePost toBase(Post post){
        if(post == null)
            return null;
        BasePost basePost = new BasePost();
        BeanUtils.copyProperties(post,basePost);
        BaseUser user = basePost.getBaseUser();
        BeanUtils.copyProperties(post.getUser(),user);
        basePost.setBaseUser(user);
        basePost.setReplies(post.getReplies().size());
        return basePost;
    }

    public DetailPost toDetail(Post post){
        if(post == null)
            return null;
        DetailPost detailPost = new DetailPost();
        BeanUtils.copyProperties(post,detailPost);
        return detailPost;
    }
}
